/**
 * TODO
 */
package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DykggMapper;
import com.bci.pwtz.mysql.model.Dykgg;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DykggService;

/**
 * TODO
 * 
 * @author ICLNetwork Team
 * @version 3.0
 */
@Service
public class DykggServiceImpl implements DykggService {
    private DykggMapper objMapper;

    private CompanyService companyService;

    private BusinessRefService businessRefService;

    private static final Logger _logger = Logger.getLogger(PdbyqServiceImpl.class);

    public DykggMapper getObjMapper() {
        return objMapper;
    }

    public void setObjMapper(DykggMapper objMapper) {
        this.objMapper = objMapper;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

   

    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    /**
     * 新增低压开关柜信息
     */
    @Override
    public Dykgg add(Dykgg dykgg) throws PwtzDatabaseException {
        if (_logger.isDebugEnabled()) {
            _logger.debug("add:dykgg=" + dykgg.toString());
        }
        if (dykgg == null) {
            if (_logger.isDebugEnabled()) {
                _logger.debug("add,dykgg is null");
            }
            return null;
        } else {
            /*UUID uuid = UUID.randomUUID();
            dykgg.setDeviceid(uuid.toString());*/
            dykgg.setStatus(1);
            dykgg.setLastmodifiedtime(new Date());
            dykgg.setCreatedate(new Date());
        }
        try {
            objMapper.insertSelective(dykgg);
        } catch (DataAccessException _e) {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return dykgg;
    }
   
    /**
     * 删除低压开关柜信息
     */
    @Override
    public int delete(String dykggId) throws PwtzDatabaseException, CanNotDeleteException {
        int result = 0;
        if (_logger.isDebugEnabled()) {
            _logger.debug("delete:dykggId=" + dykggId);
        }
        try {
            boolean bl = businessRefService.canDelete("T_DYKGG", dykggId);
            if (!bl) {
                throw new CanNotDeleteException();
            }
            result = objMapper.deleteById(dykggId,new Date());
        } catch (DataAccessException _e) {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 修改低压开关柜信息
     */
    @Override
    public int update(Dykgg dykgg) throws PwtzDatabaseException {
        int result = 0;
        if (_logger.isDebugEnabled()) {
            _logger.debug("update:dykgg=" + dykgg.toString());
        }
        if (dykgg == null) {
            if (_logger.isDebugEnabled()) {
                _logger.debug("update,dykgg is null");
            }
            return 0;
        }else{
            dykgg.setLastmodifiedtime(new Date());
        }
        try {
            result = objMapper.updateByPrimaryKeySelective(dykgg);
        } catch (DataAccessException _e) {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 低压开关柜的load方法
     */
    @Override
    public Dykgg load(String dykggId) throws PwtzDatabaseException {
        if (_logger.isInfoEnabled()) {
            _logger.info("load:dykggId=" + dykggId);
        }
        Dykgg dykgg = null;
        try {
            dykgg = objMapper.selectByPrimaryKey(dykggId);
        } catch (DataAccessException e) {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (dykgg != null) {
            _logger.debug("load:result dykgg=" + dykgg.toString());
        }
        return dykgg;
    }

    /**
     * 低压开关柜的list查询
     */
    @Override
    public PaginationList<Dykgg> findDykgg(String devicename, Long companyid, int portalType,String[] medLineCodes,int stationDeviceType,String stationCode, String beginTime,
            String endTime, int pageSize, int pageNo) throws PwtzDatabaseException {
        PaginationList<Dykgg> paginationList = new PaginationList<Dykgg>();

        if (_logger.isInfoEnabled()) {
            _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|stationDeviceType="+stationDeviceType+"|stationCode="+stationCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Dykgg> list = null;
        int beginIndex = pageSize * pageNo - pageSize;
        String companyidStr = "";
        String[] companyids = null;
        if (portalType == PwtzConstants.PORTAL_TYPE_WEB) {
            companyidStr = companyService.findChildrensId(companyid,null);
            if (companyidStr != null && companyidStr.length() > 0) {
                companyidStr = companyidStr.substring(0, companyidStr.length() - 1);
                companyids = companyidStr.split(",");
            }
        }
        try {
            list = objMapper.selectDeviceList(devicename, companyid, companyids, portalType,medLineCodes,stationDeviceType,stationCode, beginTime, endTime,
                    beginIndex, pageSize);
        } catch (DataAccessException e) {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list != null) {
            _logger.debug("findDykgg:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage = 1;
        if (pageSize != 0) {
            try {
                totalCount = objMapper.selectDeviceCount(devicename, companyid, companyids, portalType,medLineCodes,stationDeviceType,stationCode, beginTime,
                        endTime);
            } catch (DataAccessException e) {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        } else {
            pageNo = 1;
            if (list != null && list.size() > 0) {
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

    @Override
    public PaginationList<Dykgg> selectDykggByDyzf(String stationCode,String deviceName,Long companyId,int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<Dykgg> paginationList = new PaginationList<Dykgg>();
        
        List<Dykgg> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = objMapper.selectByDyzf(stationCode, deviceName, companyId, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectDykggByDytg:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCountByDyzf(stationCode, deviceName, companyId);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
}
